package com.igeekhome.ccs.controller;

import com.igeekhome.ccs.biz.ICustomerServiceBiz;
import com.igeekhome.ccs.biz.ICustomerServiceRoleBiz;
import com.igeekhome.ccs.entity.CustomerService;
import com.igeekhome.ccs.entity.CustomerServiceTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cs")
public class CustomerServiceController {
    @Autowired
    private ICustomerServiceBiz biz;
    @Autowired
    private ICustomerServiceRoleBiz iCustomerServiceRoleBiz;

//    -------------------------梁仲轩
    /**
     *返回首页客服信息
     * @param 无
     * * @return
     */
    @PostMapping("/getCsInfo")
    @ResponseBody
    public int getCustomerServiceInfo(){
        List<Map<String, Object>> listmap=biz.getCsInfolist();
        return 1;
    }
//    -------------------------梁仲轩


//    -------------------------黄薇


    /**
     *
     * @param cs,前端的字段是：
     * csId，username,password（对应前端新密码的input）,nickName，realName, headImg，phone,和一个oldPwd(原密码)
     * * @return
     */
        @PostMapping("/update")
        @ResponseBody
        public int updateCustomer(CustomerService cs, String oldPwd){

            //需要获得当前登录的客服的username，暂时还不会？？？？
            //这个可以会
            int a;
            CustomerService getCs = biz.getOneByUsername(cs.getUsername());
            if(oldPwd!=null&&(!oldPwd.equals(getCs.getPassword()))) {
                a = 0;
                System.out.println("原密码不正确");

            }else{
                a = biz.update(cs);
            }
            return a;
    }


    /**
     * 数据报表/考勤统计
     * 获得所有的 客户服务时间
     * 已测试
     * @return
     */
    @GetMapping("/worktime/all")
    @ResponseBody
    public List<CustomerServiceTime> getWorkAll(){
        List<CustomerServiceTime> list = biz.getWorkAll();
        if(list.size()==0){
            System.out.println("为空");
        }
        return list;
    }

    /**
     * 数据报表/考勤统计
     * 根据组名获得客户服务时间
     * 已测试
     * @param 组名 group
     * @return
     */
    @GetMapping("/worktime/group")
    @ResponseBody
    public  List<CustomerServiceTime> queryWorkByGroup(@RequestParam String group){
        List<CustomerServiceTime> list = biz.queryWorkByGroup(group);
        if(list.size()==0){
            System.out.println("为空");
        }
        return list;
    }
    /**
     * 数据报表/考勤统计
     * 根据客服realName获得客户服务时间
     * 已测试
     * @param 客服名  realName
     * @return
     */
    @GetMapping("/worktime/name")
    @ResponseBody
    public  List<CustomerServiceTime> queryWorkByCs(@RequestParam  String cs){
        List<CustomerServiceTime> list = biz.queryWorkByCs(cs);
        if(list.size()==0){
            System.out.println("为空");
        }
        return list;
    }
    /**
     * 数据报表/考勤统计
     * 根据起止时间获得客户服务时间
     * 已测试
     * @param  开始时间start  终止时间end
     * @return
     */
    @GetMapping("/worktime/time")
    @ResponseBody
    public  List<CustomerServiceTime> queryWorkByTime(@RequestParam  String start,@RequestParam String end){
        //需要把前端的字符串类型时间转化为Date类型

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = null;
        Date endTime = null;
        List<CustomerServiceTime> list = null;
        try {
            startTime = simpleDateFormat.parse(start);
            endTime = simpleDateFormat.parse(end);
            list = biz.queryWorkByTime(startTime, endTime);
            if(list.size()!=0){
                return list;
            }else{
                System.out.println("起止时间查询为空");
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }



//👇张煜涵-------------------------------------

    /**
     * 对外提供查询客服所有信息
     */
    @GetMapping("/query")
    public @ResponseBody
    List<CustomerService>  query() {
        return  biz.query();
    }

    //模糊查询
    @GetMapping("/queryCsInfoByString")
    public @ResponseBody
    List<CustomerService> queryCsInfoByString(@RequestParam String str) {
    return biz.matchCsInfowithString(str);
}

    //通过id找客服信息
    @GetMapping("/queryCsInfoById")
    public @ResponseBody
    CustomerService queryCsInfoById(@RequestParam int csId){
        return biz.getOneById(csId);
    }

    //改变工作账号状态
    @PostMapping("/changeWorkState")
    public  @ResponseBody
    void changeWorkState(@RequestParam int csId ,@RequestParam String oldWorkStatus){
        biz.changeWorkState(csId,oldWorkStatus);
    }

    //通过id删除客服
    @DeleteMapping("/deleteCsById")
    public  @ResponseBody
    void deleteCsById(@RequestParam int csId){
        biz.deleteCsById(csId);
    }

    //通过id数组删客服
    @DeleteMapping("/deleteCsByList")
    public @ResponseBody
    void deleteCsByList(List<Integer> list){
        biz.deleteCsByList(list);
    }

    //新增客服
    @PostMapping("/createNewCs")
    public  @ResponseBody
    void createNewCs(@RequestBody CustomerService customerService){
        biz.createNewCs(customerService);
    }

    //编辑客服信息
    @PostMapping("/updateCs")
    public  @ResponseBody
    void updateCs(@RequestBody CustomerService customerService){
        biz.updateCs(customerService);
    }

    /**
     * 通过用户名拿ID
     */
    @PostMapping("/queryCsrByName")
    public @ResponseBody
    int queryCsrByName(@RequestBody Map<String, String> map){

        return iCustomerServiceRoleBiz.queryCsrByName(map.get("name"));
    }

//👆张煜涵-------------------------------------


}
